package cn.itqiqi.student_system_java.service.impl;

import cn.itqiqi.student_system_java.mapper.TeacherMapper;
import cn.itqiqi.student_system_java.pojo.Teacher;
import cn.itqiqi.student_system_java.pojo.TeacherVo;
import cn.itqiqi.student_system_java.service.TeacherService;
import cn.itqiqi.student_system_java.utils.ResultUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description
 * @date 2022-06-21 10:37
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ResultUtils resultUtils;

    @Override
    public String getTeacherAll(Long pageNum, Long pageSize) {
        Page<Teacher> teacherPage = new Page<>(pageNum, pageSize);
        List<Teacher> teacherAll = teacherMapper.getTeacherAll(teacherPage);
        TeacherVo teacherVo = new TeacherVo(teacherPage.getTotal(), teacherAll);
        return resultUtils.success(teacherVo);
    }

    @Override
    public String getTeacherByName(Long pageNum, Long pageSize, String name) {
        if (name == null || name.equals("") || pageNum == null || pageSize == null) {
            return resultUtils.fail(400, "参数不合法！");
        } else {
            Page<Teacher> teacherPage = new Page<>(pageNum, pageSize);
            List<Teacher> teacherAll = teacherMapper.getTeacherByName(teacherPage, name);
            TeacherVo teacherVo = new TeacherVo(teacherPage.getTotal(), teacherAll);
            return resultUtils.success(teacherVo);
        }
    }

    @Override
    public String getTeacherByClassGuide(Long pageNum, Long pageSize,String classGuide) {
        if (classGuide == null || classGuide.equals("") || pageNum == null || pageSize == null) {
            return resultUtils.fail(400, "参数不合法！");
        } else {
            Page<Teacher> teacherPage = new Page<>(pageNum, pageSize);
            List<Teacher> teacherAll = teacherMapper.getTeacherByClassGuide(teacherPage, classGuide);
            TeacherVo teacherVo = new TeacherVo(teacherPage.getTotal(), teacherAll);
            return resultUtils.success(teacherVo);
        }
    }

    @Override
    public String addTeacher(Teacher teacher) {
        if (teacher.getTname() == null || teacher.getSex() == null || teacher.getIsClassGuide() == null || teacher.getClassId() == null) {
            return resultUtils.fail(400, "参数不合法！");
        } else {
            try {
                teacherMapper.addTeacher(teacher);
                return resultUtils.success("添加成功!");
            } catch (Exception e) {
                return resultUtils.fail(406, "添加失败！");
            }
        }
    }

    @Override
    public String deleteTeacher(int tid) {
        try {
            teacherMapper.deleteTeacher(tid);
            return resultUtils.success("删除成功！");
        } catch (Exception e) {
            return resultUtils.fail(406, "删除失败！");
        }
    }

    @Override
    public String updateTeacher(Teacher teacher) {
        if (teacher.getTname() == null || teacher.getSex() == null || teacher.getIsClassGuide() == null || teacher.getClassId() == null) {
            return resultUtils.fail(400, "参数不合法！");
        } else {
            try {
                teacherMapper.updateTeacher(teacher);
                return resultUtils.success("修改成功!");
            } catch (Exception e) {
                return resultUtils.fail(406, "修改失败！");
            }
        }
    }


}
